Slack

If you want to send message using incoming webhook, you can use webhook.

Parameters

The Slack notification service configuration includes following settings:

Configuration

  1. Create Slack Application using https://api.slack.com/apps?new\_app=1 1
  2. Once application is created navigate to Enter OAuth & Permissions 2
  3. Click Permissions under Add features and functionality section and add chat:write scope. To use the optional username and icon overrides in the Slack notification service also add the chat:write.customize scope. 3
  4. Scroll back to the top, click ‘Install App to Workspace’ button and confirm the installation. 4
  5. Once installation is completed copy the OAuth token. 5

  6. Create a public or private channel, for this example my_channel

  7. Invite your slack bot to this channel otherwise slack bot won’t be able to deliver notifications to this channel

  8. Store Oauth access token in argocd-notifications-secret secret

    yaml apiVersion: v1 kind: Secret metadata: name: <secret-name> stringData: slack-token: <Oauth-access-token>

  9. Define service type slack in data section of argocd-notifications-cm configmap: service yaml apiVersion: v1 kind: ConfigMap metadata: name: <config-map-name> data: service.slack: | token: $slack-token

  10. Add annotation in application yaml file to enable notifications for specific argocd app

    yaml apiVersion: argoproj.io/v1alpha1 kind: Application metadata: annotations: notifications.argoproj.io/subscribe.on-sync-succeeded.slack: my_channel

Templates

Notification templates can be customized to leverage slack message blocks and attachments feature.

Slack - 图6

The message blocks and attachments can be specified in blocks and attachments string fields under slack field:

  1. template.app-sync-status: |
  2. message: |
  3. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  4. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  5. slack:
  6. attachments: |
  7. [{
  8. "title": "{{.app.metadata.name}}",
  9. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  10. "color": "#18be52",
  11. "fields": [{
  12. "title": "Sync Status",
  13. "value": "{{.app.status.sync.status}}",
  14. "short": true
  15. }, {
  16. "title": "Repository",
  17. "value": "{{.app.spec.source.repoURL}}",
  18. "short": true
  19. }]
  20. }]

The messages can be aggregated to the slack threads by grouping key which can be specified in a groupingKey string field under slack field. groupingKey is used across each template and works independently on each slack channel. When multiple applications will be updated at the same time or frequently, the messages in slack channel can be easily read by aggregating with git commit hash, application name, etc. Furthermore, the messages can be broadcast to the channel at the specific template by notifyBroadcast field.

  1. template.app-sync-status: |
  2. message: |
  3. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  4. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  5. slack:
  6. attachments: |
  7. [{
  8. "title": "{{.app.metadata.name}}",
  9. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  10. "color": "#18be52",
  11. "fields": [{
  12. "title": "Sync Status",
  13. "value": "{{.app.status.sync.status}}",
  14. "short": true
  15. }, {
  16. "title": "Repository",
  17. "value": "{{.app.spec.source.repoURL}}",
  18. "short": true
  19. }]
  20. }]
  21. # Aggregate the messages to the thread by git commit hash
  22. groupingKey: "{{.app.status.sync.revision}}"
  23. notifyBroadcast: false
  24. template.app-sync-failed: |
  25. message: |
  26. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  27. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
  28. slack:
  29. attachments: |
  30. [{
  31. "title": "{{.app.metadata.name}}",
  32. "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
  33. "color": "#ff0000",
  34. "fields": [{
  35. "title": "Sync Status",
  36. "value": "{{.app.status.sync.status}}",
  37. "short": true
  38. }, {
  39. "title": "Repository",
  40. "value": "{{.app.spec.source.repoURL}}",
  41. "short": true
  42. }]
  43. }]
  44. # Aggregate the messages to the thread by git commit hash
  45. groupingKey: "{{.app.status.sync.revision}}"
  46. notifyBroadcast: true

The message is sent according to the deliveryPolicy string field under the slack field. The available modes are Post (default), PostAndUpdate, and Update. The PostAndUpdate and Update settings require groupingKey to be set.